package com.itheima.tanhua.api;

import com.itheima.tanhua.api.mongo.FriendServiceApi;
import com.itheima.tanhuan.pojo.db.Friend;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import javax.jnlp.FileSaveService;

@DubboService
public class FriendServiceApiImpl implements FriendServiceApi {
    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public void save(Friend friend) {
        mongoTemplate.save(friend,"friend");
    }

    @Override
    public Boolean isFriend(Long friendId, Long uid) {
        Boolean flage=false;
        Query query=Query.query(Criteria.where("userId").is(uid).and("friendId").is(friendId));
        Query query1=Query.query(Criteria.where("userId").is(friendId).and("friendId").is(uid));
        boolean exists = mongoTemplate.exists(query, Friend.class);
        boolean exists1 = mongoTemplate.exists(query1, Friend.class);
        if (exists&&exists1){flage=true;}
        return flage;
    }
//一方不喜欢一方了
    @Override
    public Boolean isNotFriend(Long uid, Long likeUserId) {
        mongoTemplate.remove(Query.query(Criteria.where("userId").is(uid).and("likeUserId")),Friend.class);
        return true;
    }

    @Override
    public Boolean createFriend(Long friendId, Long uid) {
        Friend friend=new Friend();
        friend.setUserId(uid);
        friend.setFriendId(friendId);
        friend.setCreated(System.currentTimeMillis());
        mongoTemplate.save(friend,"friend");
        //
        Friend friend1=new Friend();
        friend1.setUserId(friendId);
        friend1.setFriendId(uid);
        friend1.setCreated(System.currentTimeMillis());
        mongoTemplate.save(friend1,"friend");
        return true;
    }
}
